package com.manage.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.manage.action.interceptor.PrivilegeInterceptor;
import com.manage.po.User;
import com.manage.service.IUserService;
import com.manage.util.PagerModel;

@Controller
@Results({   
		@Result(name = "user_list", location = "systemAdmin/user/userManage.jsp"),
		@Result(name = "user_edit", location = "systemAdmin/user/userEdit.jsp"),
		@Result(name = "user_list_lookup", location = "systemAdmin/user/user_list_lookup.jsp"),
		@Result(name = "priv_edit", location = "systemAdmin/privilege/privEdit.jsp"),
		@Result(name = "error", location = "error.jsp") })
public class UserAction extends BaseAction{
	
	private static final long serialVersionUID = 6718838822334455667L;
	@Autowired
	private IUserService userService;
	
	private PagerModel pm;
	private int pageNum = 1;
	private int numPerPage = 20;
	private String[] ids;   //删除ID
	
	
	private String userid;
	//查找内容
	private String searchId;
	private String searchName;
	private String searchCardID;
		
	private String user_id;   //修改id 
	private String editorType;
	
	private User user;

	//权限控制
	private ArrayList<String> privilege;
	private boolean[] showPrivilege;

    ////////////////////////////////////////////////////////////////////////////////
		
	@Action("usermanage")
	public String getUserList(){
		pm=userService.getUserById(userid, pageNum, numPerPage);
		return "user_list";
	}
	
	@Action("SearchUser_List")
	public String search(){
		System.out.println(searchId + "***" + searchName + "***" + searchCardID );	
		pm = userService.search(searchId, searchName, searchCardID, pageNum, numPerPage);
		return "user_list";
	}
	
	
	// 用于查找带回
	@Action("User_List_LookUp")
	public String user_list_lookup(){
		pm = userService.search(searchId, searchName, searchCardID, pageNum, numPerPage);
		return "user_list_lookup";
	}
	
	
	//////////////////////////////////////////////////////////
	@Action("User_Add")
	public String toAddUserInfo() {
		editorType="add";
		return "user_edit";
	}
	
		
	@Action("User_Modify")
	public String showModify(){
		System.out.println("---------777777777----------");
		editorType="modify";
		user = userService.getUserById(user_id);
		return "user_edit";	
	}

	@Action("User_Save")
	public String save(){
		try{
			if(editorType.equals("add")){
				userService.save(user);
				return ajaxJsonSuccessMessage("您已经添加成功!","USERMANAGE","closeCurrent");
			}else{
				userService.merge(user);
				return ajaxJsonSuccessMessage("您已经修改成功!","USERMANAGE","closeCurrent");
			}
		}catch(Exception e){
			return ajaxJsonWarningMessage("该用户帐号已被使用，请您重新填写");
		}
		
	}
	
	///////////////////////////////////////////////////////////////	
	@Action("User_BatchDelete")
	public String delete(){
		userService.batchDelete(ids);
		return ajaxJsonSuccessMessage("删除成功","","");
	}

	@Action("privilegedeit")
	public String retpwd(){
		showPrivilege=new boolean[6];
		List<String> list=PrivilegeInterceptor.getPrivilege();
		if(list!=null&&list.contains("Device_Add")){
			showPrivilege[0]=true;
		}
		if(list!=null&&list.contains("Device_Modify")){
			showPrivilege[1]=true;
		}
		if(list!=null&&list.contains("Device_BatchDelete")){
			showPrivilege[2]=true;
		}
		if(list!=null&&list.contains("Room_Add")){
			showPrivilege[3]=true;
		}
		if(list!=null&&list.contains("Room_Modify")){
			showPrivilege[4]=true;
		}
		if(list!=null&&list.contains("Room_BatchDelete")){
			showPrivilege[5]=true;
		}
		
		return "priv_edit";
	}

	
	@Action("givePrivilege")
	public String givePrivilege(){
		PrivilegeInterceptor.setPrivilege(privilege);
		System.out.println(privilege);
		return ajaxJsonSuccessMessage("权限修改成功", "PRIVILEGEDEIT", "");
	}
	
	
	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public PagerModel getPm() {
		return pm;
	}

	public void setPm(PagerModel pm) {
		this.pm = pm;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public void setPageNum(String pageNum) {
		this.pageNum = Integer.parseInt(pageNum);
	}
	
	public void setNumPerPage(String numPerPage){
		if(numPerPage!=null && !"".equals(numPerPage)){
			this.numPerPage=Integer.parseInt(numPerPage);
		}
	}
	
	public void setPageNum(String[] pageNum) {
		this.pageNum = Integer.parseInt(pageNum[0]);
	}
	
	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getSearchId() {
		return searchId;
	}

	public void setSearchId(String searchId) {
		this.searchId = searchId;
	}

	public String getSearchName() {
		return searchName;
	}

	public void setSearchName(String searchName) {
		this.searchName = searchName;
	}

	public String getSearchCardID() {
		return searchCardID;
	}

	public void setSearchCardID(String searchCardID) {
		this.searchCardID = searchCardID;
	}

	public String getUser_id() {
		return user_id;
	}

	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}

	public String getEditorType() {
		return editorType;
	}

	public void setEditorType(String editorType) {
		this.editorType = editorType;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public ArrayList<String> getPrivilege() {
		return privilege;
	}

	public void setPrivilege(ArrayList<String> privilege) {
		this.privilege = privilege;
	}

	public boolean[] getShowPrivilege() {
		return showPrivilege;
	}

	public void setShowPrivilege(boolean[] showPrivilege) {
		this.showPrivilege = showPrivilege;
	}



	////////////////////////////////////////////////////////////////////////////////
	
	

}
